//app.js
var common = require('utils/common.js')
App({
  onLaunch: function () {
    var that = this
    this.checkFullSucreen()
    // 展示本地存储能力
    // var logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs)
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(res)
        wx.request({
          url: common.baseUrl() + '/wx/login/' + res.code,
          success(res) {
            console.log(res)
            if (res.data.flag) {
              that.globalData.openId = res.data.data.openid
              if (that.openIdReadyCallback) {
                that.openIdReadyCallback(res)
              }
              //服务器端登录，获取token
              wx.request({
                url: common.baseUrl() + '/user/loginByOpenIdOrUnionId',
                method: "POST",
                header: {
                  "Content-Type": "application/x-www-form-urlencoded"
                },
                data: {
                  openUnionId: that.globalData.openId
                },
                success(res) {
                  console.log(res)
                  if (res.data.flag) {
                    //保存token到全局
                    that.globalData.token = res.data.data
                    that.getTypeInfo('member')
                    that.getTypeInfo('expendType')
                    that.getTypeInfo('incomeType')
                    that.getTypeInfo('accountType')
                    //通过token请求服务器用户信息，如果有，则userInfo信息直接保存，如果没有，则通过wx.getUserInfo返回
                    wx.request({
                      url: common.baseUrl() + '/user/userInfo',
                      header: {
                        "Authorization": that.globalData.token
                      },
                      success(res) {
                        console.log(res)
                        if (res.data.flag) {
                          that.globalData.userInfo = res.data.data
                          // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                          // 所以此处加入 callback 以防止这种情况
                          if (that.userInfoReadyCallback) {
                            that.userInfoReadyCallback(res.data.data)
                          }
                        }
                        // else {
                        //   // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                        //   wx.getUserInfo({
                        //     success: res => {
                        //       // console.log(res)
                        //       // 可以将 res 发送给后台解码出 unionId
                        //       that.globalData.userInfo = res.userInfo

                        //       // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                        //       // 所以此处加入 callback 以防止这种情况
                        //       if (that.userInfoReadyCallback) {
                        //         that.userInfoReadyCallback(res.userInfo)
                        //       }
                        //     }
                        //   })
                        // }
                      },
                      fail(res) {
                        console.log(res)
                      }
                    })

                  }
                },
                fail(res) {
                  console.log(res)
                }
              })
            }
          },
          fail(res) {
            console.log(res)
          }
        })
      }
    })
  },
  getTypeInfo: function (type) {
    var that = this
    wx.request({
      url: `${common.baseUrl()}/${type}/findByUser`,
      method: "GET",
      header: {
        // "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": that.globalData.token
      },
      // data: {
      //   userId: that.globalData.userInfo.id
      // },
      success(res) {
        console.log(res)
        if (res.data.flag) {
          //保存全局数据
          switch (type) {
            case 'member':
              that.globalData.memberList = res.data.data
              break;
            case 'expendType':
              that.globalData.expendTypeList = res.data.data
              break;
            case 'incomeType':
              that.globalData.incomeTypeList = res.data.data
              break;
            case 'accountType':
              that.globalData.accountTypeList = res.data.data
              break;

            default:
              break;
          }
        }
      },
      fail(res) {
        console.log(res)
      }
    })
  },
  wxLogin: function () {

  },
  checkFullSucreen: function () {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        console.log(res)
        // 根据 屏幕高度 进行判断
        if (res.screenHeight - res.windowHeight - res.statusBarHeight - 32 > 72) {
          that.globalData.isFullSucreen = true
        }
        // 根据手机型号匹配
        if (res.model.search('iPhone X') != -1) {
          that.globalData.isFullSucreen = true
        }
      }
    })
  },
  globalData: {
    userInfo: null,
    openId: '',
    token: '',
    isFullSucreen: false,
    memberList: [],
    expendTypeList: [],
    incomeTypeList: [],
    accountTypeList: []
  }
})